import platform = require('../../../../platform/common/baseCommon');
import appClient = require('../../../../platform/client/appClient');
import platformControls = require('../../../../platform/client/controls');
import report = require('../../../../platform/client/report');

let moduleName = "platform.test";


@platform.addon("柱状图", "柱状图", moduleName)
@appClient.template("project/test/views/test/barView.html")
export class barView extends appClient.baseView {

    /**
     * 数据获取器
     */
    fetcher?: platform.IDataService;

    $search?: JQuery = undefined;
    $countryname?: JQuery = undefined;
    $district?: JQuery = undefined;
    $tableReport?: JQuery = undefined;

    private startIndex?: number = 0;
    private maxCount?: number = 100;

    onAfterBind?() {
        let that = this;
        this.$search.click(() => {
            //this.$search.button('loading');
            this.$tableReport.find('tbody').html("");
            that.onSearch();
        });
    }

    /**
     * 查询
     */
    onSearch?() {
        let countryname = this.$countryname.val();
        let district = this.$district.val();
        let params = {};
        if (countryname) {
            params['name'] = countryname;
        }
        if (district) {
            params['district'] = district;
        }

        try {
            this.fetcher
                .query("queryTest", params, this.startIndex, this.maxCount)
                .then((dataTable: platform.dataTable) => {
                    if (dataTable && dataTable.rows && dataTable.rows.length > 0) {
                        let hasThead = this.$tableReport.find('th');
                        let tbody = $('<tbody></tbody>');
                        if (hasThead.length == 0) {
                            let thead = $('<thead></thead>');
                            let tr = $('<tr></tr>');
                            for (let key in dataTable.columns) {
                                //if (key == platform.proto) break;
                                let column = <platform.dataColumn>dataTable.columns[key];
                                let th = $(`<th>${column.caption}</th>`);
                                th.appendTo(tr);
                            }
                            tr.appendTo(thead).appendTo(this.$tableReport);
                        }
                        for (let index in dataTable.rows) {
                            //if (index == platform.proto) break;
                            let dataTr = $('<tr></tr>');
                            for (let key in dataTable.rows[index].rowValues) {
                                //if (key == platform.proto) break;
                                let td = $(`<td>${(dataTable.rows[index].rowValues)[key]}</td>`);
                                td.appendTo(dataTr);
                            }
                            dataTr.appendTo(tbody);
                        }
                        tbody.appendTo(this.$tableReport);
                    }
                    // this.$search.button('reset');
                });
        } catch (error) {
            console.error(error.message);
            this.$search.button('reset');
        }
    }
}

